package com.redrose.audioandvideo.runnabletask;


import com.redrose.audioandvideo.utils.LogUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Desc: 子线程 -- 保存照片
 * Author: RedRose
 * Date: 2019/4/1
 * Email: yinsxi@163.com
 */

public class SaveImageRunnable implements Runnable {
    private String mFileName;
    private byte [] mData;

    public SaveImageRunnable(byte[]data, String fileName) {
        this.mFileName = fileName;
        this.mData = data;
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(mFileName));
            fos.write(mData);
            fos.flush();
            long currentTime = System.currentTimeMillis();
            long result = currentTime - startTime;
            LogUtils.d("SaveImageRunnable", "图片保存本地成功 耗时 = "+result + " ms");
        } catch (IOException e) {
            e.printStackTrace();
            LogUtils.e("SaveImageRunnable", "图片保存本地失败");
        }finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
